import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_member/routes/application.dart';
import 'package:flutter_member/utils/over_scroll_behavior.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:parse_server_sdk/parse_server_sdk.dart';

class PersonalCenterScreen extends StatefulWidget {
  @override
  _PersonalCenterScreenState createState() => _PersonalCenterScreenState();
}

class _PersonalCenterScreenState extends State<PersonalCenterScreen> {
  ParseUser user;

//  會員對象
  var member = ParseObject("Member");

  /// 获得会员信息
  Future<void> getMemberInfo() async {
    final ParseUser user = await ParseUser.currentUser() as ParseUser;
    var querymember = QueryBuilder<ParseObject>(ParseObject('_User'))
      ..whereEqualTo('objectId', user.objectId)
      ..includeObject(['member']);
    var rep = await querymember.query();
    if (rep.statusCode == 200 && rep.results != null) {
      setState(() {
        member = rep.results.first['member'];
        print(member);
      });
    }
  }

  Future<void> initUser() async {
    user = await ParseUser.currentUser();
  }

  @override
  void initState() {
    initUser();
    getMemberInfo();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0, //取消頂部陰影
        automaticallyImplyLeading: false, //取消默認返回箭頭
      ),
      body: ScrollConfiguration(
          behavior: OverScrollBehavior(),
          child: Container(
            child: Column(
              children: <Widget>[
                //頭像
                _headWidget(
                    netImg: member.get("avatar") != null
                        ? member.get("avatar")['url']
                        : "undefined",
                    userName: member.get("displayName")),
                //積分/餘額
                Container(
                  padding: EdgeInsets.only(
                      top: ScreenUtil().setHeight(20)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      ItemColumn(
                          title: member != null
                              ? member.get('credit').toString()
                              : '0',
                          description: '積分',
                          onTop: () => Application.jump(context,
                              "/pages/personal_center/membershippage/")),
                      //餘額
                      SplitLine(),
                      ItemColumn(
                          title: member != null
                              ? "\$${member.get('balance')}".toString()
                              : '\$ 0',
                          description: '餘額',
                          onTop: () => Application.jump(context, "/balance")),
                    ],
                  ),
                ),
                //設置選項
                Center(
                  child: Container(
                    width: ScreenUtil().setWidth(187),
                    padding: EdgeInsets.only(
                        top: ScreenUtil().setHeight(20)),
                    child: Column(
                      children: <Widget>[
                        setting('我的課程', user != null ? '/myCourse' : '/login',
                            'images/my_course.png'),
                        setting(
                          '訂單紀錄',
                          user != null ? '/index/subpage/order' : '/login',
                          'images/order.png',
                        ),
                        setting('聯絡我們', '/contactUs', 'images/service.png'),
                        setting('設置', '/settings', 'images/settings.png'),
                      ],
                    ),
                  ),
                )
              ],
            ),
          )),
    );
  }

  ///  積分和餘額上下分佈Item
  Widget ItemColumn(
      {@required String title,
        @required String description,
        VoidCallback onTop}) {
    return InkWell(
      onTap: onTop,
      child: Container(
        width: ScreenUtil().setWidth(120),
        child: GestureDetector(
          onTap: onTop,
          child: Column(
            children: <Widget>[
              Container(
                child: Text(
                  title,
                  style: TextStyle(
                      fontSize: ScreenUtil().setSp(21),
                      color: Color(0Xff154D7C),
                      fontWeight: FontWeight.bold),
                ),
              ),
              Container(
                child: Text(
                  description,
                  style: TextStyle(
                      fontSize: ScreenUtil().setSp(12),
                      color: Color(0Xff7897B2),
                      fontWeight: FontWeight.bold),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  ///頭像
  Widget _headWidget({String netImg, String src, String userName}) {
    return Container(
      width: ScreenUtil().setWidth(197),
      child: GestureDetector(
        child: Container(
          child: Column(
            children: <Widget>[
              Container(
                height: 55,
                width: 55,
                child: netImg != "undefined"
                    ? CircleAvatar(
                  backgroundImage: NetworkImage(netImg),
                  radius: 55,
                )
                    : CircleAvatar(
                  backgroundColor: Colors.transparent,
                  backgroundImage: AssetImage('images/avatar.png'),
                  radius: 55,
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
                    child: Text(
                      userName != null ? userName : '用戶名稱',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(14),
                        color: Color(0Xff2D7FC7),
                        fontFamily: "SemiBold",
                      ),
                    ),
                  ),
                  Container(
                    padding:
                    EdgeInsets.fromLTRB(ScreenUtil().setWidth(6), 10, 0, 0),
                    child: Image.asset(
                      'images/icons-more.png',
                      width: ScreenUtil().setWidth(4),
                      height: ScreenUtil().setHeight(8),
                    ),
                  )
                ],
              ),
            ],
          ),
        ),
        onTap: ()=>Application.jump(context, "/login"),
      ),
    );
  }

  //  分割線
  Widget SplitLine() {
    return Container(
        width: 1,
        height: ScreenUtil().setHeight(44),
        decoration: BoxDecoration(
          border: Border(
            right: BorderSide(
                color: Color(0Xff7897B2), width: 1, style: BorderStyle.solid),
          ),
        ));
  }

// 設置組件
  Widget setting(String title, String routes, String src) {
    return InkWell(
      child: Row(
        children: <Widget>[
          Container(
            margin: EdgeInsets.all(12),
            child: Image.asset(
              src,
              width: 20,
              height: 20,
            ),
          ),
          Text(
            title,
            style: TextStyle(
              fontSize: ScreenUtil().setSp(14),
              color: Color(0Xff154D7C),
              fontWeight: FontWeight.bold,
              fontFamily: "SemiBold",
            ),
          ),
        ],
      ),
      onTap: () {
        //Navigator.pushNamed(context,routes);
        Application.jump(context, routes);
      },
    );
  }
}
